/*
 * Justin Prentice
 * TCSS 305
 * Tetris gui - Color interpreter
 */

package view;

import java.awt.Color;
import java.util.HashMap;
import java.util.Map;

import model.Block;

/**
 * A class to keep track of which colors should be used for drawing tetris blocks.
 * @author Justin Prentice
 * @version 1.0
 */
public class ColorInterpreter
{
  /**
   * The amount to change colors by each tick.
   */
  private static final int COLOR_CHANGE_AMOUNT = 3;
  
  /**
   * The largest possible rgb value.
   */
  private static final int MAXIMUM_RGB_VALUE = 256;
    
  /**
   * The block-color associations.  
   */
  private final Map<Block, Color> my_colors;
  
  /**
   * The direction each rgb value should be changed in for each block.
   */
  private final Map<Block, int[]> my_color_vectors;
  
  /**
   * The integer tracking color changes (from original colors).
   */
  private int my_color_offset;

  /**
   * Constructs a color interpreter.
   */
  public ColorInterpreter()
  {
    super();
    my_colors = new HashMap<Block, Color>();
    my_color_vectors = new HashMap<Block, int[]>();
    my_color_offset = 0;
    setColors();
  }
  
  /**
   * Populates the map of colors.
   */
  private void setColors()
  {
    my_colors.put(Block.I, Color.cyan);
    my_colors.put(Block.J, Color.blue);
    my_colors.put(Block.L, Color.magenta);
    my_colors.put(Block.O, Color.yellow);
    my_colors.put(Block.S, Color.green);
    my_colors.put(Block.T, Color.pink);
    my_colors.put(Block.Z, Color.red);
    
    final int[] directions = {1, 1, 1};
    
    my_color_vectors.put(Block.I, directions.clone());
    my_color_vectors.put(Block.J, directions.clone());
    my_color_vectors.put(Block.L, directions.clone());
    my_color_vectors.put(Block.O, directions.clone());
    my_color_vectors.put(Block.S, directions.clone());
    my_color_vectors.put(Block.T, directions.clone());
    my_color_vectors.put(Block.Z, directions.clone());
  }  
  
  /**
   * Morphs the colors slightly.
   */
  public void tick()
  {
    for (Map.Entry<Block, Color> entry: my_colors.entrySet())
    {
      final Color color = entry.getValue();
      final Block block = entry.getKey();

      //sets red vector
      int rvector = my_color_vectors.get(block)[0];
      if (color.getRed() + rvector >= MAXIMUM_RGB_VALUE || 
          color.getRed() + rvector <= 0)
      {
        rvector = -rvector;
        my_color_vectors.get(block)[0] = rvector;
      }
      
      //sets green vector
      int gvector = my_color_vectors.get(block)[1];
      if (color.getGreen() + gvector >= MAXIMUM_RGB_VALUE ||
          color.getGreen() + gvector <= 0)
      {
        gvector = -gvector;
        my_color_vectors.get(block)[1] = gvector;
      }
      
      //sets blue vector
      int bvector = my_color_vectors.get(block)[2];
      if (color.getBlue() + bvector >= MAXIMUM_RGB_VALUE || 
          color.getBlue() + bvector <= 0)
      {
        bvector = -bvector;
        my_color_vectors.get(block)[2] = rvector;
      }
      
      my_colors.put(block, new Color(color.getRed() + rvector,
                                     color.getGreen() + gvector, 
                                     color.getBlue() + bvector));
      
    }
      
      
      
      
    my_color_offset = (my_color_offset + COLOR_CHANGE_AMOUNT) % MAXIMUM_RGB_VALUE;
  }
  
  /**
   * Resets the color offsets to default values.
   */
  public void reset()
  {
    setColors();
  }
  
  /**
   * Decides what color should be used to draw a block.
   * @param the_block the block to be interpreted
   * @return the color
   */
  public Color interpretColor(final Block the_block)
  {
    return my_colors.get(the_block);
  }
  
}
